<template>
  <div class="content-wrapper">
    <div class="nav">
      <el-breadcrumb>
        <el-breadcrumb-item :to="{ path: '/administration-assetsSetting' }">固定资产</el-breadcrumb-item>
        <el-breadcrumb-item>资产设定</el-breadcrumb-item>
      </el-breadcrumb>
      <!-- <span><el-button type="primary" @click="showAddOrEditArgumentDialog(false, false)">新增参数</el-button></span> -->
    </div>
    <div class="content">
      <el-form label-width="80px" label-position="top">
        <el-form-item v-for="type in typeList" :key="type.id">
          <template slot="label">
            <span class="tag-name" @click="showAddOrEditArgumentDialog(type, false)" v-if="isAuth('hr:dict:update')">{{type.name}}</span>
            <span class="tag-name" v-else>{{type.name}}</span>
          </template>
          <div class="tag-wrapper">
            <el-tag v-for="tag in type.children" :key="tag.id" :closable="canDel" @close="delArgument(tag.id)" type="info">
              <span class="tag-name" @click="showAddOrEditArgumentDialog(tag, type)" v-if="isAuth('hr:dict:update')">{{tag.name}}</span>
              <span class="tag-name" v-else>{{tag.name}}</span>
            </el-tag>
            <i class="el-icon-circle-plus-outline" @click="showAddOrEditArgumentDialog(false, type)" v-if="isAuth('hr:dict:save')"></i>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <el-dialog :title="!form.id ? '新增参数' : '编辑参数'" :visible.sync="dialogVisible" :close-on-click-modal="false" width="500px">
      <el-form ref="form" :model="form" label-width="80px" label-position="left">
        <el-form-item label="参数名称" prop="name" :rules="[{ required: true, message: '请输入或选择参数名称', trigger: ['blur'] }]">
          <el-input v-model="form.name" placeholder="请输入参数名称" v-if="form.pid !== '1320636431732109314' && !ifFirstLevel"></el-input>
          <el-input v-model="form.name" placeholder="请输入参数名称" v-if="ifFirstLevel" maxlength="10" show-word-limit></el-input>
          <div v-if="form.pid === '1320636431732109314'">
            <el-popover
              ref="menuListPopover"
              placement="bottom-start"
              trigger="click">
              <el-tree
                :data="departmentList"
                :props="{ label: 'name', children: 'children' }"
                node-key="id"
                ref="tree"
                @current-change="menuListTreeCurrentChangeHandle"
                :default-expand-all="true"
                :highlight-current="true"
                :expand-on-click-node="false">
              </el-tree>
            </el-popover>
            <el-input v-model="form.name" v-popover:menuListPopover disabled placeholder="点击选择部门">
              <el-button slot="append">选择</el-button>
            </el-input>
          </div>
        </el-form-item>
        <el-form-item label="上级参数" v-if="!ifFirstLevel" prop="pid" :rules="[{ required: true, message: '请选择上级参数', trigger: 'blur' }]">
          <el-select v-model="form.pid" filterable placeholder="请选择上级参数" style="width: 100%;">
            <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" style="display: flex; justify-content: space-between;">
        <div><el-button @click="delArgument(form.id)" size="mini" type="danger" v-if="form.id && canDel && !ifFirstLevel">删除</el-button></div>
        <div>
          <el-button @click="dialogVisible = false" size="mini">取消</el-button>
          <el-button type="primary" @click="submitForm" size="mini">保存</el-button>
        </div>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import formUtil from '@/utils/formUtil'
import { treeDataTranslate } from '@/utils'
export default {
  data () {
    return {
      dialogVisible: false,
      form: {
        name: '',
        value: '',
        pid: '',
        remark: '',
        sortNum: ''
      },
      departmentList: [],
      typeList: [],
      ifFirstLevel: false,
    };
  },
  components: {
  },
  computed: {
    canDel() {
      return this.isAuth('hr:dict:delete')
    }
  },
  mounted () {
    this.getDepartmentList()
    this.getDictList()
  },
  methods: {
    async getDepartmentList() {
      let list = await this.$httpN.postB('sys/dept/listAll')
      this.departmentList = treeDataTranslate(list, 'id', 'pid')
    },
    async getDictList() {
      let {list} = await this.$httpN.postB('hr/dict/list', { pid: '1320636169097375745', limit: 999 })
      if(typeof list === 'undefined' || typeof list === 'boolean') return
      list.forEach(item => {
        item.children = [];
        this.getSubDictList(item)
      })
      this.typeList = list;
    },
    async getSubDictList(item) {
      let {list} = await this.$httpN.postB('hr/dict/list', { pid: item.id, limit: 999 })
      item.children.push(...list)
    },
    showAddOrEditArgumentDialog(tagInfo, typeBelong) {
      formUtil.resetData(this.form);
      if(this.$refs.form) this.$refs.form.clearValidate();
      this.ifFirstLevel = typeBelong ? false : true
      if(tagInfo) {
        this.form = JSON.parse(JSON.stringify(tagInfo))
      } else {
        this.form.pid = typeBelong ? typeBelong.id : '1320636169097375745'
      }
      this.dialogVisible = true
    },
    async submitForm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (!await this.$httpN.postB(!this.form.id ? 'hr/dict/save' : 'hr/dict/update', this.form)) return
          formUtil.submitSuccess('保存成功');
          this.dialogVisible = false;
          this.getDictList()
        }
      });
    },
    delArgument(id) {
      formUtil.submitConfirm('请问您是否确定删除该参数？', async ()=> {
        if(!await this.$httpN.postB('hr/dict/delete', [id], false)) return;
        formUtil.submitSuccess('删除成功');
        this.dialogVisible = false;
        this.getDictList();
      })
    },
    // 菜单树选中
    menuListTreeCurrentChangeHandle (data, node) {
      this.form.name = data.name
      this.form.value = data.id
    },
  }
}
</script>

<style lang='scss' scoped>
.content-wrapper {
  height: 100vh;
  .nav {
    display: flex;
    height: 50px;
    line-height: 50px;
    padding: 0 20px;
    justify-content: space-between;
    border-bottom: 1px solid #DCDFE6;
    .el-breadcrumb {
      display: flex;
      align-items: center;
      .el-breadcrumb__item {
        float: none;
      }
    }
  }
  .content {
    padding: 20px;
    height: calc(100vh - 50px);
    overflow: auto;
    display: flex;
    justify-content: space-between;
    .tag-name {
      &:hover {
        color: #409EFF;
        cursor: pointer;
      }
    }
    .tag-wrapper {
      display: flex;
      flex-wrap: wrap;
      align-items: center;
      margin-left: 20px;
      .el-tag{
        margin-right: 10px;
        margin-bottom: 10px;
      }
      i {
        font-size: 16px;
        color: #1890FF;
        cursor: pointer;
        margin-bottom: 10px;
      }
    }
  }
}
</style>